<template>
	<xn-form-container
		:title="formData.id ? '编辑年假发放方案' : '增加年假发放方案'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="名称：" name="name">
				<a-input v-model:value="formData.name" placeholder="请输入名称" allow-clear />
			</a-form-item>
			<a-form-item label="适用范围：" name="orgId">
				<a-tree-select
					v-model:value="formData.orgId"
					style="width: 100%"
					:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
					placeholder="请选择部门"
					allow-clear
					tree-default-expand-all
					:tree-data="treeData"
					multiple
					:tree-default-expanded-keys="treeDefaultExpandedKeys"
					:field-names="{
						children: 'children',
						label: 'name',
						value: 'id'
					}"
				/>
			</a-form-item>
			<a-form-item label="员工类型：" name="userType">
				<a-checkbox-group v-model:value="formData.userType" placeholder="请选择员工类型" :options="userTypeOptions" />
			</a-form-item>
			<a-form-item label="计算规则" name="computeRule">
				<!-- <a-input v-model:value="formData.computeRule" placeholder="请输入计算规则" allow-clear /> -->
				<a-row :gutter="10" class="form-row">
					<a-col :span="7" class="form-row-con"> 满几年 </a-col>
					<a-col :span="7" class="form-row-con"> 多少天年假 </a-col>
					<a-col :span="3" class="form-row-con"> 操作 </a-col>
				</a-row>
				<div v-for="(ruleInfo, index) in computeRuleJson" class="form-div">
					<a-row :gutter="10">
						<a-col :span="7">
							<a-form-item :rules="{ required: true, message: '请输入' }">
								<a-input type="number" v-model:value="ruleInfo.year" placeholder="请输入" />
							</a-form-item>
						</a-col>
						<a-col :span="7">
							<a-form-item :rules="{ required: true, message: '请输入' }">
								<a-input type="number" v-model:value="ruleInfo.day" placeholder="请输入" />
							</a-form-item>
						</a-col>
						<a-col :span="3" style="margin-top: 4px">
							<a-button size="small" type="primary" danger ghost @click="delDomains(index)">移除</a-button>
						</a-col>
					</a-row>
				</div>
			</a-form-item>
			<a-form-item>
				<a-button type="dashed" style="width: 60%" @click="addDomains"><plus-circle-two-tone /> 添加规则 </a-button>
			</a-form-item>
			<a-form-item label="入职折算：" name="convertType">
				<a-radio-group v-model:value="formData.convertType" placeholder="请选择入职折算" :options="obversions" />
			</a-form-item>
			<a-form-item label="延长有效期至次年：" name="effectiveType">
				<a-date-picker
					format="MM-DD"
					v-model:value="formData.effectiveType"
					value-format="MM-DD"
					placeholder="请选择有效期"
					style="width: 100%"
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="sysAnnualLeaveForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import sysAnnualLeaveApi from '@/api/biz/sysAnnualLeaveApi'
	import userApi from '@/api/sys/userApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const userTypeOptions = ref([])
	const treeDefaultExpandedKeys = ref([])
	const treeData = ref([])
	const computeRuleJson = ref([])
	const obversions = tool.dictList('OBVERSION_RULE')

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			recordData.userType = JSON.parse(recordData.userType)
			formData.value = Object.assign({}, recordData)
			formData.value.orgId = formData.value.orgId.split(',')
			computeRuleJson.value = JSON.parse(formData.value.computeRule)
		}
		userTypeOptions.value = tool.dictList('COMPANY_STAFF_TYPE')
		nextTick(() => {
			// 机构选择器数据
			userApi.userOrgTreeSelector().then((res) => {
				if (res !== null) {
					treeData.value = res
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === '0') {
							treeDefaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									treeDefaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			})
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		computeRuleJson.value = []
		formData.value = {}
		visible.value = false
	}
	// 信息增行
	const addDomains = () => {
		if (computeRuleJson.value === null) {
			computeRuleJson.value = []
		}
		computeRuleJson.value.push({
			year: 0,
			day: 0
		})
	}
	// 删减行
	const delDomains = (index) => {
		computeRuleJson.value.splice(index, 1)
	}
	// 默认要校验的
	const formRules = {
		name: [required('请输入名称')],
		orgId: [required('请输入适用范围')],
		userType: [required('请输入员工类型')]
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			formDataParam.userType = JSON.stringify(formDataParam.userType)
			formDataParam.computeRule = JSON.stringify(computeRuleJson.value)
			formDataParam.orgId = formDataParam.orgId.join(',')
			sysAnnualLeaveApi
				.sysAnnualLeaveSubmitForm(formDataParam, formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
